package com.cloudkeeper.leasing.identity.controller.impl;

import com.cloudkeeper.leasing.base.model.Result;
import com.cloudkeeper.leasing.identity.controller.VillageCadresTemporaryController;
import com.cloudkeeper.leasing.identity.domain.VillageCadresTemporary;
import com.cloudkeeper.leasing.identity.dto.villagecadrestemporary.VillageCadresTemporaryDTO;
import com.cloudkeeper.leasing.identity.dto.villagecadrestemporary.VillageCadresTemporarySearchable;
import com.cloudkeeper.leasing.identity.service.VillageCadresTemporaryService;
import com.cloudkeeper.leasing.identity.vo.VillageCadresTemporaryVO;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * 村干部临时表 controller
 * @author asher
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VillageCadresTemporaryControllerImpl implements VillageCadresTemporaryController {

    /** 村干部临时表 service */
    private final VillageCadresTemporaryService villageCadresTemporaryService;

    @Override
    public Result<VillageCadresTemporaryVO> findOne(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id) {
        Optional<VillageCadresTemporary> villageCadresTemporaryOptional = villageCadresTemporaryService.findOptionalById(id);
        return villageCadresTemporaryOptional.map(villageCadresTemporary -> Result.of(villageCadresTemporary.convert(VillageCadresTemporaryVO.class))).orElseGet(Result::ofNotFound);
    }

    @Override
    public Result<VillageCadresTemporaryVO> add(@ApiParam(value = "村干部临时表 DTO", required = true) @RequestBody @Validated VillageCadresTemporaryDTO villageCadresTemporaryDTO) {
        VillageCadresTemporary villageCadresTemporary = villageCadresTemporaryService.save(villageCadresTemporaryDTO.convert(VillageCadresTemporary.class));
        return Result.ofAddSuccess(villageCadresTemporary.convert(VillageCadresTemporaryVO.class));
    }

    @Override
    public Result<VillageCadresTemporaryVO> update(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id,
        @ApiParam(value = "村干部临时表 DTO", required = true) @RequestBody @Validated VillageCadresTemporaryDTO villageCadresTemporaryDTO) {
        Optional<VillageCadresTemporary> villageCadresTemporaryOptional = villageCadresTemporaryService.findOptionalById(id);
        if (!villageCadresTemporaryOptional.isPresent()) {
            return Result.ofLost();
        }
        VillageCadresTemporary villageCadresTemporary = villageCadresTemporaryOptional.get();
        BeanUtils.copyProperties(villageCadresTemporaryDTO, villageCadresTemporary);
        villageCadresTemporary = villageCadresTemporaryService.save(villageCadresTemporary);
        return Result.ofUpdateSuccess(villageCadresTemporary.convert(VillageCadresTemporaryVO.class));
    }

    @Override
    public Result delete(@ApiParam(value = "村干部临时表id", required = true) @PathVariable String id) {
        villageCadresTemporaryService.deleteById(id);
        return Result.ofDeleteSuccess();
    }

    @Override
    public Result<List<VillageCadresTemporaryVO>> list(@ApiParam(value = "村干部临时表查询条件", required = true) @RequestBody VillageCadresTemporarySearchable villageCadresTemporarySearchable,
        @ApiParam(value = "排序条件", required = true) Sort sort) {
        List<VillageCadresTemporary> villageCadresTemporaryList = villageCadresTemporaryService.findAll(villageCadresTemporarySearchable, sort);
        List<VillageCadresTemporaryVO> villageCadresTemporaryVOList = VillageCadresTemporary.convert(villageCadresTemporaryList, VillageCadresTemporaryVO.class);
        return Result.of(villageCadresTemporaryVOList);
    }

    @Override
    public Result<Page<VillageCadresTemporaryVO>> page(@ApiParam(value = "村干部临时表查询条件", required = true) @RequestBody VillageCadresTemporarySearchable villageCadresTemporarySearchable,
        @ApiParam(value = "分页参数", required = true) Pageable pageable) {
        Page<VillageCadresTemporary> villageCadresTemporaryPage = villageCadresTemporaryService.findAll(villageCadresTemporarySearchable, pageable);
        Page<VillageCadresTemporaryVO> villageCadresTemporaryVOPage = VillageCadresTemporary.convert(villageCadresTemporaryPage, VillageCadresTemporaryVO.class);
        return Result.of(villageCadresTemporaryVOPage);
    }

}